package com.lianyu507.aop;

import com.lianyu507.util.HttpClientUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * =================================
 * <p>
 * Created by Administrator on 2017/11/23.
 * <p>
 * 描述:
 */
@Aspect
@Component
public class UserWordsCountLog {

    private Logger log= LoggerFactory.getLogger(this.getClass());
    @Pointcut(value = "@annotation(com.lianyu507.annotation.UserWordsCount)")
    private void cut(){

    }

    @Around("cut()")
    public Object around(ProceedingJoinPoint point) throws Throwable{
        Object result = point.proceed();

        try {
            handle(point);
        }catch (Exception e){
            log.error("日志记录出错！",e);
        }
        return result;
    }

    private void handle(ProceedingJoinPoint point) throws  Exception{
        Signature signature = point.getSignature();
        if(!(signature instanceof MethodSignature)){
            throw new IllegalArgumentException("该方法只能用于注解");
        }
        HttpClientUtil.doGet("http://localhost:8082/ee-manager/user/words");
    }
}
